<template>
  <div>
    <el-card shadow="hover" style="margin-bottom: 20px">
      <div class="flex-warp">
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-input placeholder="请输入学生姓名"
                      size="small"
                      suffix-icon="el-icon-search"
                      v-model="listQueryParam.name"
            >
            </el-input>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-input placeholder="请输入考试名称"
                      size="small"
                      suffix-icon="el-icon-search"
                      v-model="listQueryParam.examName"
            >
            </el-input>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button size="small"
                       icon="el-icon-search"
                       @click="search"
            >搜索
            </el-button>
          </div>
        </div>

        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button type="primary"
                       size="small"
                       icon="el-icon-refresh-right"
                       @click="refresh"
            >刷新
            </el-button>
          </div>
        </div>
        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button type="success"
                       size="small"
                       icon="el-icon-plus"
                       @click="add"
            >添加
            </el-button>
          </div>
        </div>

        <div class="flex-warp-item">
          <div class="flex-warp-item-box">
            <el-button type="danger"
                       size="small"
                       icon="el-icon-folder-delete"
                       @click="batchDel"
            >删除
            </el-button>
          </div>
        </div>
      </div>
    </el-card>
    <el-card shadow="hover"
             class="mt15"
    >
      <el-table :data="tableData"
                stripe
                ref="multipleTable"
                @selection-change="handleSelectionChange"
                style="width: 100%"
      >
        <el-table-column type="selection"
                         width="55"
        >
        </el-table-column>
        <el-table-column prop="id"
                         align="center"
                         label="编号"
        >
        </el-table-column>
        <el-table-column prop="name"
                         align="center"
                         label="学生姓名"
        >
        </el-table-column>

        <el-table-column prop="createTime"
                         align="center"
                         label="创建时间"
        >
        </el-table-column>
        <el-table-column prop="endTime"
                         align="center"
                         label="考试截止时间"
        >
        </el-table-column>
        <el-table-column prop="examName"
                         align="center"
                         label="考试名称"
        >
        </el-table-column>
        <el-table-column fixed="right"
                         align="center"
                         label="操作"
        >
          <template slot-scope="scope">
            <!-- <el-button @click="addExcel(scope.row)"
                       type="text"
                       size="small"
            >自动阅卷
            </el-button> -->
            <el-button @click="addExcel(scope.row)"
                       type="info"
                       size="small"
                       icon="el-icon-s-opportunity"
            >自动阅卷
            </el-button>
            <!-- <el-button type="text"
                       size="small"
                       @click="delClick(scope.row)"
            >删除
            </el-button> -->
            <el-button type="danger" 
                      size="small"
                      icon="el-icon-delete" 
                      @click="delClick(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0"
                  :total="total"
                  :page.sync="listQueryParam.current"
                  :limit.sync="listQueryParam.pageSize"
                  @pagination="getTableDataList"
      />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog :title="dialogTitle"
               :visible.sync="addOrUpdateDialogVisible"
               width="40%"
               :before-close="handleClose"
    >
      <el-form :model="formData"
               :rules="rules"
               ref="formData"
               label-width="100px"
      >
        <el-form-item label="所属考试"
                      prop="examId"
        >
          <el-select v-model="formData.examId" clearable placeholder="请选择所属考试">
            <el-option
              v-for="item in examIdOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属学生"
                      prop="studentId"
        >
          <el-select v-model="formData.studentId" clearable placeholder="请选择所属学生">
            <el-option
              v-for="item in studentIdOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer"
      >
        <el-button @click="addOrUpdateDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitForm('formData')"
        >确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="请上传学生答题的excel文件"
               :visible.sync="showExcel"
               width="40%"
    >
      <el-form :model="formData"
               :rules="rules"
               ref="formData"
               label-width="100px"
      >
        <el-form-item label="文件">
          <file-upload v-model="formData.excel" :limit="1"></file-upload>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer"
      >
        <el-button @click="showExcel = false">取 消</el-button>
        <el-button type="primary"
                   @click="saveExcel"
        >确 定</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import { getList, insert, update, get, del, getExamDate, getStudentDate, saveData } from '@/api/ExamStudent/index'
import { getPageGrade} from '@/api/ExamDetail/index';
import Pagination from '@/components/Pagination'
import { tableUtil } from '@/mixins/table'
import { Message } from 'element-ui'
import uploadImg from '@/components/ImageUpload'
import ImgPreview from '@/components/ImgPreview'
import FileUpload from '@/components/FileUpload'

export default {
  mixins: [tableUtil],
  components: {
    FileUpload,
    Pagination, uploadImg, ImgPreview
  },
  data() {
    return {
      type: localStorage.getItem('examType'),
      listQueryParam: {
        current: 1,
        pageSize: 5
      },
      showExcel: false,
      formData: {},
      rules: {
        examId: [
          { required: true, message: '请选择所属考试', trigger: 'change' }
        ],
        studentId: [
          { required: true, message: '请选择所属学生', trigger: 'change' }
        ]
      },
      examIdOptions: [],
      studentIdOptions: [],
      currentStudent: {}
    }
  },
  mounted() {
    this.getTableDataList()
    this.getExamDateList()
    this.getStudentDateList()
  },
  methods: {
    saveExcel() {
      if (!this.formData.excel) {
        Message.error('请先上传文件')
        return
      }
      let param = {}
      param.url = this.formData.excel
      param.examId = this.currentStudent.examId
      param.studentId = this.currentStudent.studentId
      saveData(param).then((res) => {
        this.formData.excel = ''
        Message.success('系统阅卷成功！')
        this.showExcel = false;
        this.getGradeData(param.examId, param.studentId);
      })
    },
    getGradeData(examId, studentId) {
      getPageGrade({ examId, studentId }).then((res) => {
        if (res && res.data) {
          // 使用 console.log 打印成绩数据
          console.log('成绩数据：', res.data.records);
          Message.success('成绩加载成功！');
        } else {
          Message.error('未获取到成绩数据');
        }
      }).catch((error) => {
        Message.error('成绩加载失败：' + error.message);
      });
    },
    addExcel(row) {
      this.showExcel = true
      this.currentStudent = row
    },
    getTableDataList() {
      if (this.type == 1) {
        this.listQueryParam.teacherId = localStorage.getItem('examId')
      }
      getList(this.listQueryParam).then((res) => {
        this.total = res.data.total
        this.tableData = res.data.records
      })
    },
    updateClick(row) {
      this.dialogTitle = '修改'
      get(row.id).then((res) => {
        this.formData = res.data
        this.addOrUpdateDialogVisible = true
      })
    },
    delByIds(ids) {
      del(ids).then((res) => {
        Message.success('删除成功')
        this.getTableDataList()
      })
    },
    saveData() {
      if (this.formData.id) {
        update(this.formData).then((res) => {
          Message.success('修改成功')
          this.addOrUpdateDialogVisible = false
          this.getTableDataList()
        })
      } else {
        insert(this.formData).then((res) => {
          Message.success('添加成功')
          this.addOrUpdateDialogVisible = false
          this.getTableDataList()
        })
      }
    },
    getExamDateList() {
      getExamDate().then(res => {
        this.examIdOptions = res.data
      })
    },
    getStudentDateList() {
      getStudentDate().then(res => {
        this.studentIdOptions = res.data
      })
    }
  }
}
</script>
